package com.smbms.servlet;

import com.smbms.mapper.UserMapper;
import com.smbms.pojo.Role;
import com.smbms.pojo.User;
import com.smbms.util.PageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User login(String userCode, String userPassword) {
        return userMapper.login(userCode,userPassword);
    }

    @Override
    public PageBean<User> getPage(Map<String,Object> map) {

        PageBean<User> pageBean = new PageBean<>();
        pageBean.setCurrentPage((Integer) map.get("index"));
        pageBean.setPageSize((Integer) map.get("pagesize"));
        map.put("startIndex",pageBean.getStartIndex());
        /*查询总记录数*/
        pageBean.setTotalCount(userMapper.getCount(map));
        /*查询用户列表*/
        pageBean.setResult(userMapper.getUserList(map));
        return pageBean;
    }

    @Override
    public List<Role> getRoleList() {
        return userMapper.getRoleList();
    }

    @Override
    public User checkUserCode(String userCode) {
        return userMapper.checkUserCode(userCode);
    }



    /*新增方法油事务，所以加上事务注解*/
    @Transactional
    @Override
    public boolean userAddSave(User user) {
        if(userMapper.userAddSave(user) > 0){
            return true;
        }
        return false;
    }

    @Override
    public User getUser(Integer userId) {
        return userMapper.userView(userId);
    }

    @Override
    public boolean modifyUser(User user) {
        if(userMapper.modifyUser(user) > 0){
                return true;
        }
        return false;
    }

    @Override
    public int deleteUserById(Integer userId) {
        return userMapper.deleteUserById(userId);
    }

}
